<?php
$output = array();

if (!isset($_GET["searchWord"])) {
    echo("No search parameter specified");
    exit();
} else {
    $searchWord = $_GET["searchWord"];
}

if (isset($_GET["searchField"])) {
    $searchField = $_GET["searchField"];
}

if (isset($_GET["searchProfile"])) {
    $searchProfile = $_GET["searchProfile"];
    $searchProfile = str_replace('+', ' ', $searchProfile);
}


// import database connection variables and connectToMysql method
require_once __DIR__ . '/db_config.php';

$mysqli = connectToMysql();

if (isset($searchField)) {

    if ($searchField == "companyName") {
        if (isset($searchProfile)) {
            $query = "SELECT job.id, job.name, job.description, job.salary, job.companyId, job.jobUrl, job.active, job.postedDate, location.latitude, location.longitude FROM Job job
         left join Location location on job.location = location.id JOIN Company company ON job.companyId = company.id where company.name like '%" . $searchWord . "%'"
                . " and company.profile='" . $searchProfile . "'";
        } else {
            $query = "SELECT job.id, job.name, job.description, job.salary, job.companyId, job.jobUrl, job.active, job.postedDate, location.latitude, location.longitude FROM Job job
         left join Location location on job.location = location.id JOIN Company company ON job.companyId = company.id where company.name like '%" . $searchWord . "%'";
        }
    } else {
        if (isset($searchProfile)) {
            $query = "select job.id, job.name, job.description, job.salary, job.companyId, job.jobUrl, job.active, job.postedDate, location.latitude, location.longitude from Job left join Location location on job.location = location.id JOIN Company company ON job.companyId = company.id where " . $searchField . " like '%" . $searchWord . "%'"
                . " and company.profile='" . $searchProfile . "'";
        } else {
            $query = "select job.id, job.name, job.description, job.salary, job.companyId, job.jobUrl, job.active, job.postedDate, location.latitude, location.longitude from Job left join Location location on job.location = location.id JOIN Company company ON job.companyId = company.id where " . $searchField . " like '%" . $searchWord . "%'"
                . " and company.profile='";
        }
    }
} else {
    if (isset($searchProfile)) {
        $query = "SELECT job.id, job.name, job.description, job.salary, job.companyId, job.jobUrl, job.active, job.postedDate, location.latitude, location.longitude FROM Job job left join Location location on job.location = location.id JOIN Company company ON job.companyId = company.id where ( company.name like '%" . $searchWord . "%'"
            . " or job.name like '%" . $searchWord . "%'"
            . " or job.description like '%" . $searchWord . "%' )"
            . " and company.profile='" . $searchProfile . "'";
    } else{
        $query = "SELECT job.id, job.name, job.description, job.salary, job.companyId, job.jobUrl, job.active, job.postedDate, location.latitude, location.longitude FROM Job job left join Location location on job.location = location.id JOIN Company company ON job.companyId = company.id where ( company.name like '%" . $searchWord . "%'"
            . " or job.name like '%" . $searchWord . "%'"
            . " or job.description like '%" . $searchWord . "%' )";
    }
}

//echo $query;
if ($result = $mysqli->query($query)) {
    /* fetch object array */
    while ($row = $result->fetch_array()) {

//        print_r($row);

        $output[] = array_map('utf8_encode', $row);
//        $output[] = $row;

    }

    /* free result set */
    $result->close();
}

/* close connection */
$mysqli->close();
//echo serialize($output);
// this will print the output in json
//echo serialize($output);
$finalResult = json_encode($output, JSON_NUMERIC_CHECK);
$finalResult = str_replace('"salary":""', '"salary":NULL', $finalResult);
$finalResult = str_replace('"active":""', '"active":NULL', $finalResult);
$finalResult = str_replace('"active":0', '"active":true', $finalResult);
$finalResult = str_replace('"active":1', '"active":false', $finalResult);
$finalResult = str_replace('"latitude":""', '"latitude":NULL', $finalResult);
$finalResult = str_replace('"longitude":""', '"longitude":NULL', $finalResult);


//echo json_last_error();
echo $finalResult;
?>